﻿import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

@SuppressWarnings("serial")
public class LoginFrame extends JFrame
{
	public static final int WIDTH  = 200 ;				//畫面寬度
	public static final int HEIGHT = 300 ;				//畫面高度
	JTextArea userText ;								//輸入user name的地方
	public Function F ;
	Font wordFont = new Font( "標楷體" , Font.CENTER_BASELINE , 20 ) ;		//字型大小
	User NowUser ;															//進入的使用者
	LoginFrame( )
	{
		super( ) ;
		setSize( WIDTH,HEIGHT ) ;											//設定視窗大小
		setTitle( "登入頁" ) ;												//設定標題
		setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ;					//設定關閉
		setResizable( false ) ;												//不能改變式窗大小
		Dimension dim = getToolkit().getScreenSize(); //取得螢幕大小
		setLocation(dim.width/2-getWidth()/2, dim.height/2 - getHeight()/2);
		
		
		setLayout( new GridLayout( 5 , 0 ) ) ;								//設置Layout
		
		
		//etLayout(new FlowLayout(FlowLayout.CENTER));
		MaxLengthTextDocument maxLength = new MaxLengthTextDocument( ) ;	//username 最大長度
		maxLength.setMaxChars( 15 ) ;

		userText = new JTextArea( "" , 1 , 15 ) ;					//輸入user name的地方				
		userText.setFont( wordFont ) ;
		userText.setDocument( maxLength ) ;
			
		JPanel jp1 = new JPanel( ) ;								//用來裝textarea				
		jp1.add( userText ) ;

		JButton loginBt = new JButton( "登入" ) ;						//登入按鈕
		loginBt.addActionListener( new loginBt_act( ) ) ;
		
		JButton registBt = new JButton( "註冊" ) ;					//註冊按鈕
		registBt.addActionListener( new registBt_act( ) ) ;
		
		JPanel jp2 = new JPanel( ) ;								//用來裝兩個Button
		jp2.add( loginBt ) ;
		jp2.add( Box.createHorizontalStrut( 20 ) ) ;
		jp2.add( registBt ) ;
		
		this.add(Box.createVerticalStrut( 30 ) ) ;					//排版
		this.add( jp1 ) ;
		//this.add( Box.createVerticalStrut( 20 ) ) ;
		//this.add( Box.createHorizontalStrut( 60 ) ) ;
		this.add( jp2 ) ;
		//MainClass mp = new MainClass( ) ;
		//mp.setVisible( true ) ;
		F = new Function( ) ;
	}
	
	private class loginBt_act implements ActionListener
	{
		/* 設置建構式，結論:程式一開啟便會跑到這個Listener
		public loginBt_act( )
		{
			JOptionPane.showMessageDialog( null , "============\n" ) ;
		}
		*/
		@Override		
		public void actionPerformed( ActionEvent e ) 
		{
			//Login lg = new Login();
			//boolean a = lg.CheckNameOK( 1 ,userText.getText( ) ) ;
			if( F.CheckNameOK( userText.getText( ) ) == 1 )
			{	
				if( ReadData( 1 , userText.getText( ) ) )
				{
					JOptionPane.showMessageDialog( null , "登入成功!! *^_^* \n" ) ;
					MainClass mp = new MainClass( userText.getText( ) ) ;
					mp.setVisible( true ) ;
					dispose();
					//setVisible( false ) ;
				}
				else
				{
					JOptionPane.showMessageDialog( null , "無此帳號喔!! 0_0 \n" ) ;
				}
			}
			else
			{
				JOptionPane.showMessageDialog( null , "此帳號名稱不合法呦!! >_0 \n" ) ;
				//alert( "123" ) ;
			}
		}
	}
	
	private class registBt_act implements ActionListener
	{
		@Override
		public void actionPerformed( ActionEvent e ) 
		{
			//Login lg = new Login( ) ;
			//boolean a = lg.CheckNameOK( 1 , userText.getText( ) ) ;
			if( F.CheckNameOK( userText.getText( ) ) == 1 )
			{	
				if( ReadData( 2 , userText.getText( ) ) )
				{
					WriteData( new User( userText.getText( ) ) ) ;
					JOptionPane.showMessageDialog( null , "帳號已新增完成，自動登入!! *^_^* \n" ) ;
					MainClass mp = new MainClass( userText.getText( ) ) ;
					mp.setVisible( true ) ;
					dispose();
					//setVisible( false ) ;
				}
				else
				{
					JOptionPane.showMessageDialog( null , "此帳號已經被用過了呦!! 0_0 \n" ) ;
				}
			}
			else
			{
				JOptionPane.showMessageDialog( null , "此帳號名稱不合法呦!! >_0 \n" ) ;
			}
		}
	}

	public boolean CheckNameOK( int Chosen , String name )
	{
		//Name is illigal or not
		if( name.length( ) <= 0 || name.length( ) > 10 )
			return false ;
		//Is Login, check data
		//Checking Name is OK in the file or not
		//OK, Read User Infomation
		return ReadData( Chosen , name ) ;
	}
	
	//創建新使用者資料
	public synchronized void WriteData( User OkUser )
	{
		PrintWriter outputStream = null;
		try
		{
			//取得檔案尾端
			outputStream = new PrintWriter( new FileOutputStream( "UserSet.txt" , true ) ) ;
		}
		catch( FileNotFoundException e )
		{
			JOptionPane.showMessageDialog( null, "WriteData　Error : AllMessage.txt 檔案不存在 請先建置！！" ) ;
			System.exit( 0 ) ;
		}
		//寫入最新一筆資訊
		outputStream.println( OkUser.getUserName( ) + "\t" + OkUser.getUserBuildTime( ) ) ;
		outputStream.close( ) ;
	}
	
	//讀取資料判定有無
	public boolean ReadData( int Chosen , String CheckName )
	{
		Scanner inputStream = null;
		try 
		{
			inputStream = new Scanner( new FileInputStream( "UserSet.txt" ) ) ;		
		}
		catch( FileNotFoundException e )
		{
			System.out.println( "ReadData　Error : UserSet.txt 檔案有問題！！" ) ;
			System.exit( 0 ) ;
		}
		String A , B ;
		String [] Token ;
		String [] Save = new String [ 2 ] ;
		while( inputStream.hasNextLine( ) )
		{
			A = inputStream.nextLine( ) ;
			//一個使用者: "名稱\t創見日期\t"
			B = A ;
			Token = B.split( "\t" ) ;
			int i = 0 ;
			for( String Tk : Token )
			{
				Save[ i ] = Tk ;
				//Debugger..
				//System.out.println( "x" + i + Save[ i ] + "x" ) ;
				i++ ;
			}
			if( Chosen == 1 && Save[ 0 ].contentEquals( CheckName ) == true )
			{
				NowUser = new User( Save[ 0 ] , Save[ 1 ] ) ;
				inputStream.close( ) ;
				return true ;
			}
			else if( Chosen == 2 && Save[ 0 ].contentEquals( CheckName ) == true )
			{
				inputStream.close( ) ;
				return false ;
			}
		}
		inputStream.close( ) ;
		if( Chosen == 1 )
			return false ;
		else
			return true ;
	}
}



